clear all 
set more off 
set maxvar 15000 
clear matrix


    foreach dep in hs_ed coll_ed yrsschool_bin  {
        
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
        keep if baseline_sample==1 

    * Decade (grouped)
        gen group =1 if decade==1910 | decade==1920 
        replace group =2 if decade==1930 | decade==1940
        replace group =3 if decade==1950 | decade==1960 | decade==1970
        tab decade group, m
        
    * Dependent variable 
        if "`dep'"=="hs_ed" | "`dep'"=="coll_ed" {
            gen dep =`dep'*100
        }
        else {
            gen dep = `dep'
        }
        
        
        tempfile fulldata
        save `fulldata'
        
    
    * Binscatter by time period
        forvalues g = 1(1)3 {   
            if `g'==1 local period "group1"
            if `g'==2 local period "group2"
            if `g'==3 local period "group3"
        
            binscatter dep rank_father_baseline if group==`g' [aw=wgt_sex_race],  nq(20) savedata("binscatter_`period'") replace    
        }

    * Save binscatter
        forvalues g = 1(1)3 {   

            if `g'==1 local period "group1"
            if `g'==2 local period "group2"
            if `g'==3 local period "group3"

            insheet using "binscatter_`period'.csv", clear 
            gen group = `g'
            save "binscatter_`period'.dta", replace 
        }
    
    * Append
        use binscatter_group1, clear
        append using binscatter_group2
        append using binscatter_group3
    
    * Regressions on the full panel; save constant and slope 
        gen slope=.
        gen intercept=.
        
            forvalues g = 1(1)3 {   
    
                preserve
                use `fulldata', clear

                reg dep rank_father_baseline if group==`g' [aw=wgt_sex_race]
                restore
                
                    local number = round(_b[rank_father_baseline], 0.01)
                    local number`g': display %-04.2fc `number'
                        
                replace slope = _b[rank_father_baseline] if group==`g' 
                replace intercept = _b[_cons] if group==`g' 
                
            }
        
    
    * Generate predicted values for each point using slope and intercept
        gen yhat = intercept + (slope*rank_father_baseline)

    * Figures
        if "`dep'"=="hs_ed" {
            local title "Respondent completed high school"
            local pos 5
        }
        if "`dep'"=="coll_ed" {
            local title "Respondent completed college"
            local pos 11
        }
        if "`dep'"=="yrsschool_bin" {
            local title "Years of schooling"
            local pos 5
        }

        #delimit ;
            twoway (scatter dep rank_father_baseline if group==1, m(oh) mc(ebblue))
                   (line yhat rank_father_baseline if group==1, lp(dash) lc(ebblue)) 
               
                   (scatter dep rank_father_baseline if group==2, m(+) mc(dknavy)) 
                   (line yhat rank_father_baseline if group==2, lp(dash) lc(dknavy))
               
                   (scatter dep rank_father_baseline if group==3, m(dh) mc(green)) 
                   (line yhat rank_father_baseline if group==3, lp(dash) lc(green)),
               
            legend(on order(1 3 5) rows(3) label(1 "1910-1929 cohorts (Slope=`number1')") label(3 "1930-1949 cohorts (Slope=`number2')") 
            label(5 "1950-1979 cohorts (Slope=`number3')") ring(0) pos(`pos'))
            xti(" " "Predicted parental rank") yti("`title'" " ")  ;
            graph export "$Mydirectory2/appendix_a/`dep'_period_3lines.pdf", replace ; 
        #delimit cr
    
    
    * Get rid of all binscatter-related files
        forvalues g = 1(1)3 {
            if `g'==1 local period "group1"
            if `g'==2 local period "group2"
            if `g'==3 local period "group3"

            cap rm "binscatter_`period'.csv" 
            cap rm "binscatter_`period'.do" 
            cap rm "binscatter_`period'.dta" 
            cap rm "binscatter_`period'.csv.do" 

        }

    }